package com.jackrain.nea.fc.vp.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author xuxisnhuai
 */
@Mapper
public interface ViewCvipopenMapper {


    /**
     * 查询VIP类型ID
     *
     * @param ecode
     * @return
     */
    @Select("SELECT ID FROM VP_C_VIPTYPEGROUP WHERE ECODE = #{ecode}")
    Long selecttypegroupid(String ecode);

    /**
     * 查询VIP等级表ID
     *
     * @param ecode
     * @return
     */
    @Select("SELECT ID FROM VP_C_VIPTYPE WHERE ECODE = #{ecode}")
    Long selecttypeid(String ecode);


    @Update("UPDATE VP_C_VIP_ACC SET CP_C_STORE_ID =#{arg0}  WHERE ID=#{arg1}")
    Integer updatevipacc(Long storeid, Long accid);

    /**
     * 根据传入的数据查找店仓档案表中ECODE=传入值的ID
     *
     * @param ecode
     * @return
     */
    @Select("SELECT ID FROM VP_C_VIP_ACC WHERE ECODE = #{arg0} AND VP_C_VIPTYPEGROUP_ID=#{arg1} AND VP_C_VIP_ID =#{arg2}")
    Long selectvipaccid(String ecode, Long typeid,Long vipid);



    //---------->>>F4
    @Select("SELECT ECODE FROM VP_C_VIPTYPE WHERE ID = #{viptypeid}")
    String selectviptypeecode(Long viptypeid);

    @Select("SELECT ECODE FROM vp_c_viptypegroup WHERE ID = #{viptypeid}")
    String selectviptypegroupecode(Long viptypeid);


    @InsertProvider(type = CVipOpenSqlProvider.class, method = "insertSql")
    /**
     *插入数据
     * @param jsonObject
     * @return
     */
    int insert(JSONObject jsonObject);

    @UpdateProvider(type = CVipOpenSqlProvider.class, method = "updateSql")
    /**
     *更新数据
     * @param jsonObject
     * @return
     */
    int update(JSONObject jsonObject);

    /**
     * 新增
     *
     * @param list
     */
    @InsertProvider(type = CVipOpenSqlProvider.class,
            method = "insertCvipbatch")
    void insertbatch(@Param("list") List<HashMap> list);

    //删除
    /***
     * 删除颜色数据
     * @param vipopenid
     * @return int
     */
    @Delete("DELETE FROM VP_C_VIPOPEN WHERE ID=#{vipopenid}")
    int deletevipOpen(long vipopenid);


    /**
     * 查询
     *
     * @param param
     * @return List<HashMap>
     */
    @SelectProvider(type = CVipOpenSqlProvider.class,
            method = "selectvipopensql")
    List<HashMap> selectvipopensql(@Param("list") Object[] param);


    class CVipOpenSqlProvider {

        public String selectvipopensql(final HashMap map) {
            return new SQL() {
                {

                    String colum = " VP_C_VIPTYPEGROUP_ID,VP_C_VIPTYPE_ID,ENAME,ECODE,SEX, BIRTHDATE,MOBIL,CP_C_STORE_ID, ADDRESS, JOB,REMARK,ISRECEIVEPROM,RECEIVEWAY,WECHAT,EMAIL ,OPENID,ID_CARD,ACADEMIC , ISACTIVE,SOURCE ";

                    SELECT(colum);

                    FROM("VP_C_VIPOPEN");
                    Object[] list = (Object[]) map.get("list");
                    StringBuilder sb = new StringBuilder("(");
                    for (Object num : list) {
                        sb.append(num).append(",");
                    }
                    sb.deleteCharAt(sb.length() - 1).append(")");
                    WHERE("ID IN " + sb.toString());

                }
            }.toString();
        }

        public String insertCvipbatch(HashMap map) {
            List<HashMap> list = (List<HashMap>) map.get("list");
            StringBuilder sb = new StringBuilder();
            StringBuilder keySb = new StringBuilder();
            sb.append("INSERT INTO VP_C_VIPOPEN(");
            Set<String> keySet = list.get(0).keySet();
            for (String key : keySet) {
                sb.append(key + ",");
                keySb.append("#'{'list[{0}]." + key + "},");
            }
            keySb.deleteCharAt(keySb.length() - 1);
            sb.deleteCharAt(sb.length() - 1);
            sb.append(") VALUES ");

            MessageFormat mf = new MessageFormat("(" + keySb.toString() + ")");
            for (int i = 0; i < list.size(); i++) {
                sb.append(mf.format(new Object[]{i}));
                if (i < list.size() - 1) {
                    sb.append(",");
                }
            }

            return sb.toString();
        }

        public String updateSql(JSONObject map) {
            return new SQL() {
                {
                    UPDATE("VP_C_VIP");
                    for (String key : map.keySet()) {
                        String id = "ID";
                        if (!id.equals(key)) {
                            SET(key + "= #{" + key + "}");
                        }
                    }

                    WHERE("ID = #{ID}");
                }

            }.toString();
        }

        public String insertSql(JSONObject jo) {
            return new SQL() {
                {
                    INSERT_INTO("VP_C_VIPOPEN");
                    for (String key : jo.keySet()) {
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }

    }
}
